package com.kuaihuoyun.ctms.api;

import com.kuaihuoyun.ctms.dto.*;
import com.kuaihuoyun.ctms.entity.CustomerOrderRequest;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.http.Body;
import retrofit2.http.POST;
import retrofit2.http.Query;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by lxji on 2018/3/26.
 */
public class CustomerOrderService extends BaseService {

    public CustomerOrderService(String url, String token) {
        super(url, token);
    }

    public CtmsResponse<List<String>> createCustomerOrder(List<CustomerOrderRequest> orders) throws IOException {
        Call<CtmsResponse<List<String>>> responseCall =
                retrofit.create(CustomerOrderApi.class).createCustomerOrder(token,
                        CreateCustomerOrderRequest.builder().orders(orders).build());
        final Response<CtmsResponse<List<String>>> response = responseCall.execute();
        if(!response.isSuccessful()) {
            return CtmsResponse.create(response);
        }

        return response.body();
    }

    public CtmsResponse<Boolean> cancelCustomerOrders(List<String> numbers) throws IOException {
        Call<CtmsResponse<Boolean>> responseCall = retrofit.create(CustomerOrderApi.class).cancelCustomerOrders(token,
                CancelCustomerOrderRequest.builder().numbers(numbers.stream().collect(Collectors.joining(","))).build());
        final Response<CtmsResponse<Boolean>> response = responseCall.execute();
        if(!response.isSuccessful()) {
            return CtmsResponse.create(response);
        }

        return response.body();
    }

    public CtmsResponse<QueryCustomerOrderResult> queryAllCustomerOrders(QueryAllCustomerOrderRequest request) throws IOException {
        Call<CtmsResponse<QueryCustomerOrderResult>> responseCall = retrofit.create(CustomerOrderApi.class).queryAllCustomerOrders(token, request);
        final Response<CtmsResponse<QueryCustomerOrderResult>> response = responseCall.execute();
        if(!response.isSuccessful()) {
            return CtmsResponse.create(response);
        }

        return response.body();
    }

    interface CustomerOrderApi {
        @POST("/customer_order/create_customer_order")
        Call<CtmsResponse<List<String>>> createCustomerOrder(@Query("access_token") String token, @Body CreateCustomerOrderRequest request) throws IOException;

        @POST("/customer_order/cancel_customer_orders")
        Call<CtmsResponse<Boolean>> cancelCustomerOrders(@Query("access_token") String token, @Body CancelCustomerOrderRequest request) throws IOException;

        @POST("/customer_order/query_customer_orders")
        Call<CtmsResponse<QueryCustomerOrderResult>> queryAllCustomerOrders(@Query("access_token") String token, @Body QueryAllCustomerOrderRequest request) throws IOException;
    }
}
